package com.wf.generator;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


public class GeneratorView {
	String path = this.getClass().getResource("").getPath();
	String srpath = path.substring(0, path.indexOf("target")) + "\\src\\main\\resources\\template";
	String scpath = path.substring(0, path.indexOf("target")) + "\\src\\main\\template";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		try {
			System.out.println("请输入实体类名称");
			BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
			String entity = strin.readLine();
			Class<?> classType = Class.forName("com.umessage.entity." + entity);
			Field[] fields = classType.getDeclaredFields();
			GeneratorView two = new GeneratorView();
			two.scindex(entity, fields);
			two.sctoolbar(entity, fields);
			two.scdetail(entity, fields);
			two.scedit(entity, fields);
			two.sctable(entity, fields);
			two.scController(entity, fields);
			System.out.println("生成成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void scindex(String entity, Field[] fields) {
		System.out.println(path);
		String srwjpath = srpath + "/index.jsp";
		String scwjpath = scpath + "/index.jsp";
		copyFile(srwjpath, scwjpath, null);
	}

	public void sctoolbar(String entity, Field[] fields) {
		String srwjpath = srpath + "/toolbar.jsp";
		String scwjpath = scpath + "/toolbar.jsp";
		copyFile(srwjpath, scwjpath, null);
	}

	public void scdetail(String entity, Field[] fields) {
		String srwjpath = srpath + "/detail.jsp";
		String scwjpath = scpath + "/detail.jsp";
		StringBuffer detailfields = new StringBuffer();
		String lowerwentity=entity.substring(2, entity.length()).toLowerCase();
		for (Field field : fields) {
			if(!field.getName().equals("id")){
			detailfields.append("\t<tr>\r\n");
			detailfields.append("\t\t<td>\\${fieldLabel['" + field.getName()+ "']}</td>\r\n");
			detailfields.append("\t\t<td>\\${" +lowerwentity + "."+ field.getName() + "}</td>\r\n");
			detailfields.append("\t</tr>\r\n");
			}
		}
		Map<String,String> contents=new HashMap<String, String>();
		contents.put("detailfields", detailfields.toString());
		copyFile(srwjpath, scwjpath,contents);
	}

	public void scedit(String entity, Field[] fields) {
		String srwjpath = srpath + "/edit.jsp";
		String scwjpath = scpath + "/edit.jsp";
		StringBuffer editfields = new StringBuffer();
		String lowerwentity=entity.substring(2, entity.length()).toLowerCase();
		for (Field field : fields) {
			if(!field.getName().equals("id")){
			editfields.append("\t<tr>\r\n");
			editfields.append("\t\t<td>\\${fieldLabel['" + field.getName()+ "']}</td>\r\n");
			editfields.append("\t\t<td><input class='easyui-textbox' type='text' name='"+lowerwentity+ "."+ field.getName()+ "' required value='\\${"+lowerwentity+ "." + field.getName() + "}'></input></td>\r\n");
			editfields.append("\t</tr>\r\n");
			}
		}
		Map<String,String> contents=new HashMap<String, String>();
		contents.put("lowerwentity", entity.substring(2, entity.length()).toLowerCase());
		contents.put("lowerentity", entity.toLowerCase());
		contents.put("editfields", editfields.toString());
		copyFile(srwjpath, scwjpath, contents);
	}

	public void sctable(String entity, Field[] fields) {
		String srwjpath = srpath + "/table.jsp";
		String scwjpath = scpath + "/table.jsp";
		StringBuffer tablefields = new StringBuffer();		
		for (Field field : fields) {
			if(!field.getName().equals("id")){
			tablefields.append("\t\t<th data-options='field:\""+ field.getName() + "\",width:100'>\\${fieldLabel['"+ field.getName() + "']}</th>\r\n");
			}
		}
		Map<String,String> contents=new HashMap<String, String>();
		contents.put("tablefields", tablefields.toString());
		copyFile(srwjpath, scwjpath,contents);
	}

	public void scController(String entity, Field[] fields) {
		String srwjpath = srpath + "/Controller.template";
		String scwjpath = scpath + "/"+entity+"Controller.java";
		StringBuffer Controllerfields = new StringBuffer();
		for (Field field : fields) {
			Controllerfields.append("\t\tmap.put(\""+ field.getName() + "\",\""+ field.getName() + "\");\r\n");
		}
		Map<String,String> contents=new HashMap<String, String>();
		contents.put("lowerwentity", entity.substring(2, entity.length()).toLowerCase());
		contents.put("lowerentity",entity.toLowerCase());
		contents.put("upperentity",entity);
		contents.put("Controllerfields",Controllerfields.toString());	
		copyFile(srwjpath, scwjpath,contents);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public static void copyFile(String oldFile, String newFile,Map<String,String> contents) {
		try {
			FileReader fr = new FileReader(oldFile);
			FileWriter fw = new FileWriter(newFile);

			BufferedReader br = new BufferedReader(fr);
			String line = null;
			while ((line = br.readLine()) != null) {
				if (contents!=null) {
					for(Map.Entry<String, String> entry: contents.entrySet()) {
					line = line.replaceAll(entry.getKey(), entry.getValue());
					}
				}
				fw.write(line + "\r\n");
			}
			br.close();
			fw.close(); // 关闭数据流
			fr.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}